package com.lx.base.config.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.
        RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;

import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit;



@Slf4j
public class RedisUtils  {


    @Autowired
	protected RedisTemplate redisTemplate;



    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 批量删除key
     *
     * @param pattern
     */
    @SuppressWarnings("unchecked")
    public void removePattern(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0) {
            redisTemplate.delete(keys);
        }
    }

    /**
     * 删除对应的value
     *
     * @param key
     */
    @SuppressWarnings("unchecked")
    public void remove(final String key) {
        if (hasKey(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    @SuppressWarnings("unchecked")
    public boolean hasKey(final String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    @SuppressWarnings("unchecked")
    public Object get(final String key) {
        Object result = null;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        return result;
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    @SuppressWarnings("unchecked")
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            log.error( "RedisUtil.set", "policy服务reids保存异常！", "msg:{0},key:{1},value:{2}",e.getMessage(),key,value);
        }
        return result;
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    @SuppressWarnings("unchecked")
    public boolean set(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            log.error("RedisUtil.set", "policy服务reids保存异常！", "msg:{0},key:{1},value:{2},expireTime:{3}",e.getMessage(),key,value,expireTime);
        }
        return result;
    }

    public boolean expire(final String key, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            log.error("RedisUtil.set", "policy服务reids保存异常！", "msg:{0},key:{1},expireTime:{3}",e.getMessage(),key,expireTime);
        }
        return result;
    }

    @SuppressWarnings("unchecked")
    public boolean set(final String key, Object value, Long expireTime, TimeUnit timeUnit) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, timeUnit);
            result = true;
        } catch (Exception e) {
            log.error("RedisUtil.set", "policy服务reids保存异常！", "msg:{0},key:{1},value:{2},expireTime:{3}",e.getMessage(),key,value,expireTime);
        }
        return result;
    }

    /**
     *
     * @Title: getListSize
     * @Description: 获取redis list 数据类型   长度
     * @param key
     * @return    Long    返回类型
     */
    public Long getListSize(String key){
        Long size = 0L;
        try {
            size = redisTemplate.opsForList().size(key);
        } catch (Exception e) {
            log.error("RedisUtil.push", "redis getListSize 异常", "key:{0},捕获异常:{1}",key, e.getMessage());
        }
        return size;
    }

    /**
     * 自增序列
     * @param key
     * @return
     */
    public Long incr(String key){
        RedisConnectionFactory redisConnectionFactory=redisTemplate.getConnectionFactory();
        RedisAtomicLong redisAtomicLong=new RedisAtomicLong(key,redisConnectionFactory);
        return redisAtomicLong.incrementAndGet();
    }
}
